package com.hunlian.makelove.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.hunlian.makelove.AppApplication;
import com.hunlian.makelove.R;
import com.hunlian.makelove.bean.RegisterBean;
import com.hunlian.makelove.common.AppConfig;
import com.hunlian.makelove.common.StringUtil;
import com.hunlian.makelove.net.NetVisitCallback;
import com.hunlian.makelove.net.api.RegisterServiceVisit;
import com.hunlian.makelove.tools.MD5Tools;
import com.hunlian.makelove.tools.MyToast;
import com.hunlian.makelove.view.CustomProgressDialog;
import com.hunlian.makelove.widget.address.AppManager;

public class RegisterActivity extends BaseActivity{

	private RegisterActivity mSelf;
	private Button mBtnRegister;
	private EditText mEtPassword;
	private ImageView mIvEye,mIvDelete;
	private boolean mDisplayFlg = false;
	private CustomProgressDialog dialog;
	private String mStrPhone;
	private String mFrom;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mSelf = this;
		setContentView(R.layout.register);
		
		mFrom = getIntent().getStringExtra("from");
		
		ImageButton mIbBack = (ImageButton) findViewById(R.id.back);
		mIbBack.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				mSelf.finish();
			}
			
		});
		TextView mTvTitle = (TextView) findViewById(R.id.title);
		mTvTitle.setText("设置密码");
		
		mStrPhone = this.getIntent().getStringExtra("phone");
		
		mEtPassword = (EditText) findViewById(R.id.et_password);
		mIvEye = (ImageView) findViewById(R.id.iv_eye);
		mIvDelete = (ImageView) findViewById(R.id.iv_delete);
		mBtnRegister = (Button) findViewById(R.id.btn_register);
		
		setListener();
	}

	private void setListener() {
		mIvEye.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				if (!mDisplayFlg) {
					// display password text, for example "123456"
					mEtPassword
							.setTransformationMethod(HideReturnsTransformationMethod
									.getInstance());
				} else {
					// hide password, display "."
					mEtPassword
							.setTransformationMethod(PasswordTransformationMethod
									.getInstance());
				}
				mEtPassword.setSelection(mEtPassword.getText().length());
				mDisplayFlg = !mDisplayFlg;
				mEtPassword.postInvalidate();
			}
			
		});
		
		mIvDelete.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				mEtPassword.setText("");
			}
			
		});
		
		mBtnRegister.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {

				if (TextUtils.isEmpty(mEtPassword.getText().toString())) {
					Toast.makeText(mSelf, "请输入密码",
							Toast.LENGTH_SHORT).show();
					return;
				}
				if (StringUtil.ifchinese(mEtPassword.getText().toString())) {
					Toast.makeText(mSelf,
							"请输入6-16位数字、字母和符号", Toast.LENGTH_SHORT).show();
					return;
				}
				if (StringUtil.length(mEtPassword.getText().toString().trim()) < 6
						|| StringUtil.length(mEtPassword.getText().toString()
								.trim()) > 16) {
					Toast.makeText(mSelf,
							"请输入6-16位数字、字母和符号", Toast.LENGTH_SHORT).show();
					return;
				}

				if (!AppApplication.networkAvailable) {
					MyToast.showShort("没有网络");
					return;
				}
				
				dialog  = CustomProgressDialog
						.createDialog(mSelf, "正在加载中...");
				dialog.show();
				
				String isForget = "";
				if(mFrom.equals("register")){
					isForget = "0";
				}else if(mFrom.equals("forget")){
					isForget = "1";
				}

				RegisterServiceVisit visit = new RegisterServiceVisit(mSelf);
				visit.setParamters(mStrPhone,
						MD5Tools.getInstance().getMD5(mEtPassword.getText().toString()),
						isForget);
				//System.out.println("注册/找回密码请求参数：" + visit.getParamter());
				visit.getResult(new NetVisitCallback<RegisterBean,Object>(mSelf){

					@Override
					public void onSuccess(RegisterBean data) {
						if (dialog != null && dialog.isShowing()) {
							try {
								dialog.dismiss();
								dialog = null;
							} catch (Exception e) {
							}
						}

						if(data==null){

						}else{
							//Printout.println("注册/找回密码 反馈成功" + serviceReturn);
							String userId = data.getUserId();
							AppConfig.getInstance().setStringValue("userId",userId);
							//关闭之前所有Activity
							AppManager.getAppManager().finishAllActivity();
							if(mFrom.equals("forget")){
								//如果是找回密码就进入登录
								startActivity(new Intent(mSelf,LoginActivity.class));
							}else if(mFrom.equals("register")){
								//如果是第一次注册就进入完善资料
								startActivity(new Intent(mSelf,OrganizingDataActivity.class));
							}
							AppConfig.getInstance().setLoginedTel(mStrPhone);
							mSelf.finish();
						}

					}

					@Override
					public void onFailure(String errorEvent, String message,Object obj) {
						super.onFailure(errorEvent, message,obj);
						if (dialog != null && dialog.isShowing()) {
							try {
								dialog.dismiss();
								dialog = null;
								Toast.makeText(mSelf, message, Toast.LENGTH_SHORT).show();
							} catch (Exception e) {
							}
						}
					}

				});

				
			}
			
		});
	}
	
}
